'''
Created on Nov 19, 2010

@author: namikata
'''

import pyglet
from pyglet import font
from gameobjects.vector2 import Vector2
from game.base.GameEntity import GameEntity, get_rectangle
from gl.gl import GL_TRIANGLES

COLOR_WHITE         = (255,255,255)
COLOR_BLACK         = (0,0,0)
COLOR_RED           = (255,0,0)
COLOR_GREEN         = (0,255,0)
COLOR_BLUE          = (0,0,255)
COLOR_YELLOW        = (255,255,0)
COLOR_LIGHT_BLUE    = (0,255,255)

class UI_Player(GameEntity):
    '''
    classdocs
    '''    

    parent = None
    min = 0
    armor = 0
    armor_max = 0
    energy = 0
    energy_max = 0
    cooldown = 0
    cooldown_max = 0
    charge = 0
    charge_max = 0
    name = None
    w2, h2 = (0,0)
    is_self = None
    font = None
    text = None

    def __init__(self,objclass, objsclass, parent,image, is_self = False):
        '''
        Constructor
        '''
        name = parent.name+"_ui"
        GameEntity.__init__(self, parent.world, name, image, objclass, objsclass)
        
        self.parent = parent
        self.sprite.position = parent.sprite.position
        self.min = 0
        self.armor = int(parent.armor)
        self.armor_max = int(parent.armor_max)
        self.energy = int(parent.armory.energy)
        self.energy_max = int(parent.armory.energy_max)
        self.cooldown = int(parent.armory.cooldown)
        self.cooldown_max = int(parent.armory.cooldown_max)
        self.charge = int(parent.armory.charge)
        self.charge_max = int(parent.armory.charge_max)
        self.name = name
        self.w2, self.h2 = (parent.sprite.width,parent.sprite.height)
        self.is_self = is_self
        self.font = font.load('Verdana', 12)
        self.text = font.Text(self.font)
        self.text.text = self.parent.name
                
    def update(self,elapsed, keyboard, mouse):       
        self.sprite.position = self.parent.sprite.position
        self.armor = self.parent.armor
        self.energy = self.parent.armory.energy
        self.cooldown = self.parent.armory.cooldown
        self.cooldown_max = self.parent.armory.cooldown_max
        self.charge = self.parent.armory.charge
        self.charge_max = self.parent.armory.charge_max        
        
    def draw_ui_bar(self, step, value, value_max, position, w,h, color):
        fill_percentage = int((value/value_max)*100)

        pp = Vector2(position[0]-51,position[1]-40) #initial pivot position of bars
        vs = -5 #vertical steps for multiple bars
        op = (pp.x, pp.y)
        ip = (pp.x+1,pp.y+1)
        
        outer = get_rectangle(op[0],op[1]+(vs*step),w,h,COLOR_WHITE)
        inner = get_rectangle(ip[0],ip[1]+(vs*step),w-2,h-2,COLOR_BLACK)
        fill = get_rectangle(ip[0],ip[1]+(vs*step),fill_percentage,h-2,color)
        pyglet.graphics.draw(3, GL_TRIANGLES, ("v2f",outer["v1"]),("c3B",outer["c1"]),)
        pyglet.graphics.draw(3, GL_TRIANGLES, ("v2f",outer["v2"]),("c3B",outer["c2"]),)
        pyglet.graphics.draw(3, GL_TRIANGLES, ("v2f",inner["v1"]),("c3B",inner["c1"]),)
        pyglet.graphics.draw(3, GL_TRIANGLES, ("v2f",inner["v2"]),("c3B",inner["c2"]),)
        pyglet.graphics.draw(3, GL_TRIANGLES, ("v2f",fill["v1"]),("c3B",fill["c1"]),)
        pyglet.graphics.draw(3, GL_TRIANGLES, ("v2f",fill["v2"]),("c3B",fill["c2"]),)
                
    def render(self):
        #name_text = self.font.render(self.parent.name,True,COLOR_WHITE)
        #w,h = name_text.get_size()
        #surface.blit(name_text,self.location + Vector2(-(w/2),50))
        
        #info bars
        w,h = (102,6) #for a 100x4 bar with a 1 width border

        #life box
        step = 0
        self.draw_ui_bar(step, self.armor, self.armor_max, self.sprite.position, w, h, COLOR_GREEN)
        
        #energy box
        step = 1
        self.draw_ui_bar(step, self.energy, self.energy_max, self.sprite.position, w,h,COLOR_LIGHT_BLUE)
        
        #cooldown box
        if(self.cooldown > 0 or self.parent.armory.shooting):
            step = 2
            cooldown = 0
            if self.parent.armory.shooting or self.cooldown_max == 0:
                self.draw_ui_bar(step, self.cooldown_max, self.cooldown_max, self.sprite.position, w,h,COLOR_RED)
            else:
                self.draw_ui_bar(step, self.cooldown, self.cooldown_max, self.sprite.position, w,h,COLOR_RED)
        #charge box
        if(self.charge > 0):
            step = 2
            self.draw_ui_bar(step, self.charge, self.charge_max, self.sprite.position, w,h,COLOR_YELLOW)